import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '手机电量',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        splashColor: Colors.transparent,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  static const platform = const MethodChannel('fauks.com/battery');

  // 当前手机电量
  int _result = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('手机电量'),
      ),
      body: Column(
        children: [
          Text("当前手机电量为：$_result"),
          ElevatedButton(
            onPressed: getBatteryInfo,
            child: Text('获取电量'),
          ),
        ],
      ),
    );
  }

  void getBatteryInfo() async {
    final int battery = await platform.invokeMethod('getBatteryInfo');
    setState(() {
      _result = battery;
    });
  }
}
